﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;
using Yoozoo.Libs;

namespace Gameplay.PVE.Survivor
{
    public abstract class ElementBase : IClass
    {
        public ElementConfig config;
        public BattleUnit source;
        public BattleUnit target;
        protected float startTime;
        public Bullet bullet;
        public SkillBase skill;
        
        private BattleEffect durationEffect;
        private int Stack = 1;
        protected void CreateHitEffect()
        {
            if (config.hit_effect > 0)
            {
                if (target == null)
                {
                    if (BattleDebug.enableLog)
                    {
                        BattleDebug.LogError(config.id + "目标为空");
                    }
                    return;
                }
                BattleEffectManager.Instance.CreateEffectAutoDispose(config.hit_effect, target.position,target.Forward,-1,target.Transform);
            }
        }

        protected void CreateDurationEffect()
        {
            if (config.duration_effect > 0)
            {
                durationEffect = BattleEffectManager.Instance.CreateEffect(config.duration_effect);
                durationEffect.gameObject.Parent = target.Transform;
                durationEffect.gameObject.Position = target.position;
            }
        }

        protected void RemoveDurationEffect()
        {
            if (durationEffect != null)
            {
                BattleEffectManager.Instance.RemoveEffect(durationEffect);
                durationEffect = null;
            }
        }

        public void Initialize(ElementConfig config,BattleUnit source, BattleUnit target,Bullet bullet)
        {
            this.config = config;
            this.source = source;
            this.target = target;
            this.bullet = bullet;
            startTime = TimeManager.time;
            CreateHitEffect();
            CreateDurationEffect();
        }

        public virtual bool NeedDispose()
        {
            var duration = config.duration;
            if (duration < 0)
            {
                return false;
            }
            return TimeManager.time - startTime >= duration;
        }
        
        public virtual float GetDuration()
        {
            return config.duration;
        }
        
        public abstract void Execute();

        public abstract void OnFree();

        public void Free()
        {
            RemoveDurationEffect();
            OnFree();
            
        }
        
        public abstract bool IsImmediate();
        public void OnReset()
        {
            Stack = 1;
        }

        public virtual void Update()
        {
            
        }

        public void addStack()
        {
            Stack++;
            startTime = TimeManager.time;
            if (config.conflict_layer_element?.Count > 0)
            {
                for (int i = 0; i < config.conflict_layer_element.Count; i++)
                {
                    var item = config.conflict_layer_element[i];
                    if (Stack >= item.x)
                    {
                        target.AddElement((int)item.y, target, bullet);
                    }
                }
            }
        }
    }
}
